<?php
namespace app\api\controller\vppz;


use app\admin\model\Admin;
use think\Cache;


use \app\admin\model\vppz\Hospital as HospitalModel;
use \app\admin\model\vppz\Service as ServiceModel;
use \app\admin\model\vppz\HospitalService as HospitalServiceModel;
use \app\admin\model\vppz\HospitalChild as HospitalChildModel;
use \app\admin\model\vppz\KeshiHospital as KeshiHospitalModel;
use \app\admin\model\vppz\Keshi as KeshiModel;

use \addons\vppz\library\Vpower;

class Hospital extends App
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();


		$this->HospitalModel = new HospitalModel;
		$this->KeshiHospitalModel = new KeshiHospitalModel;
		$this->ServiceModel = new ServiceModel;
		$this->KeshiModel = new KeshiModel;
		$this->AdminModel = new Admin;
		$this->HospitalServiceModel = new HospitalServiceModel;
		$this->HospitalChildModel = new HospitalChildModel;
    }


    /**
     * 首页
     */
    public function index(){
		$area=$this->_area;
		$hid = input('hid');

        $config = \think\App::initCommon();
		// 获取医院
		$hospital = $this->HospitalModel->where(['app_id'=>$this->app_id,'use_switch'=>1])->find($hid);
		//$hospital = pdo_fetch('SELECT * FROM ' .tablename('vp_pz_hospital') . ' where  uniacid = :uniacid AND city_id=:city_id AND id=:id AND status=1 AND is_del=0 ', array(':uniacid' => $_W['uniacid'],':city_id'=>$city['id'],':id'=>$hid));
		if(empty($hospital)){
			$this->error('该医院尚未开通');
		}
		if($hospital['use_switch']!=1){
			$this->error('该医院尚未开通相关服务');
		}
        $hospital['rank'] = $config['hospital_level'][$hospital['rank']]??'三级';
        $hospital['label'] = $config['hospital_label'][$hospital['label']]??'综合医院';
		//$hospital['avatar']=VP_IMAGE_URL($hospital['avatar']);
		//$hospital['service']=iunserializer($hospital['service']);
		$hss = $this->HospitalServiceModel->field('service_id,price,use_switch')
            ->where(['app_id'=>$this->app_id,'area_id'=>$area['id'],'hospital_id'=>$hospital['id']])->select();
        //获取科室
        $keshiHospital = $this->KeshiHospitalModel->with(['yyks'])->where(array(
            'hospitalid'=>$hid
        ))->select();
        $hospital['keshi'] = [];
        if(empty($keshiHospital) === FALSE) {
            foreach ($keshiHospital as $row) {
                $row->getRelation('yyks')->visible(['name']);
            }

            $hospital['keshi'] = $keshiHospital;
        }
        //分店

        $fendian = $this->HospitalChildModel->where(['hospital_id'=>$hid])
            ->select();
		// 获取服务
		//$services = pdo_fetchall('SELECT id,code,name,logo,intro,price FROM ' .tablename('vp_pz_service') . ' where  uniacid = :uniacid AND status=1 AND is_del=0 order by sort desc ', array(':uniacid' => $_W['uniacid']));
		$services = $this->ServiceModel->field('id,code,stype,name,logo_image,icon_image,intro,price,priceo')
            ->where(['app_id'=>$this->app_id,'use_switch'=>1])->order('weigh', 'desc')->select();
        $resService = [];
		// 根据医院设置，覆盖服务
		for($i=0;$i<count($services);$i++){
			for($j=0;$j<count($hss);$j++){
				if($services[$i]['id']==$hss[$j]['service_id']){
					//$_service=$hospital['service'][$services[$i]['id']];
					$services[$i]['use_switch']=$hss[$j]['use_switch'];
					$services[$i]['price']=$hss[$j]['price'];
					//$services[$i]['logo']=VP_IMAGE_URL($services[$i]['logo']);
                    array_push($resService,$services[$i]);
				}
			}
		}

		$this->success('',array(
			'now'=>time(),
			'hospital'=>$hospital,
			'services'=>$resService,
            'fendian'=>$fendian
		));
    }
//本地医疗
    public function bdyllists(){
		$area=$this->_area;
        $config = \think\App::initCommon();
		// 获取医院
		$hospital = $this->HospitalModel->where([
            'app_id'=>$this->app_id,
            'area_id'=>$area['id'],
            'is_top'=>1,
            'is_local'=>1,
            'use_switch'=>1,
            'is_kuajing'=>2])->select();
        if(empty($hospital) === FALSE){
            foreach ($hospital as &$v){
                $v['rank'] = $config['hospital_level'][$v['rank']]??'三级';
                $v['label'] = $config['hospital_label'][$v['label']]??'综合医院';
            }
        }
		$this->success('',array(
			'now'=>time(),
			'hospital'=>$hospital
		));
    }

    /**
     * 跨境
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function kuajinglists(){
		$area=$this->_area;
        $config = \think\App::initCommon();
		// 获取医院
		$hospital = $this->HospitalModel->where(['use_switch'=>1,'is_kuajing'=>1,'is_local'=>2])->select();
        if(empty($hospital) === FALSE){
            foreach ($hospital as &$v){
                $v['rank'] = $config['hospital_level'][$v['rank']]??'三级';
                $v['label'] = $config['hospital_label'][$v['label']]??'综合医院';
            }
        }
		$this->success('',array(
			'now'=>time(),
			'hospital'=>$hospital
		));
    }

    /**
     * 跨境科室
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function kuajingkeshilists(){
		$hospital_id = input('hospital_id');
		// 获取医院
		$keshi = $this->KeshiHospitalModel->where(['hospitalid'=>$hospital_id])->select();
        if(empty($keshi) === FALSE){
            foreach ($keshi as $k=>$v){
                $keshiDetail = $this->KeshiModel->where('id','=',$v['ksid'])->find();
                $keshi[$k]['name'] = $keshiDetail['name']??'';
            }
        }
		$this->success('',array(
			'now'=>time(),
			'keshi'=>$keshi
		));
    }
    public function listsbyid(){
        $config = \think\App::initCommon();
		$area=$this->_area;
		// 获取医院
        $search = input('keyword');
        $departmentId = input('departmentId');
        $hospitalids = [];
        if (empty($departmentId)){
            $this->error('相关科室不存在');
        }else{
            $hospitalids = $this->KeshiHospitalModel->where('ksid','=',$departmentId)->column('hospitalid');
        }
		$hospitalmodel = $this->HospitalModel->where(['app_id'=>$this->app_id,'area_id'=>$area['id'],'is_local'=>1,'use_switch'=>1,'is_kuajing'=>2]);
        $hospitalmodel = $hospitalmodel->whereIn('id',$hospitalids);
        if(empty($search) === FALSE){
            $hospitalmodel = $hospitalmodel->whereLike('name|intro','%'.$search.'%');
        }
        $hospitallist = $hospitalmodel->select();
        if(empty($hospitallist) === FALSE){
            foreach ($hospitallist as &$v){
                $v['rank'] = $config['hospital_level'][$v['rank']]??'三级';
                $v['label'] = $config['hospital_label'][$v['label']]??'综合医院';
            }
        }
		$this->success('',array(
			'now'=>time(),
			'hospital'=>$hospitallist
		));
    }
    public function hospitaldetail(){
        $hid = input('hid');
        $area=$this->_area;
		// 获取医院
		$hospital = $this->HospitalModel->find($hid);
		if(empty($hospital)){
			$this->error('该医院尚未开通');
		}
        $config = \think\App::initCommon();
        $hospital['rank'] = $config['hospital_level'][$hospital['rank']]??'三级';
        $hospital['label'] = $config['hospital_label'][$hospital['label']]??'综合医院';
        if($hospital['has_coupon'] == 1 || $hospital['has_zhekou_coupon'] == 1){
            $hospital['has_coupon'] = 1;
        }
        $hss = $this->HospitalServiceModel->field('service_id,price,use_switch')
            ->where(['app_id'=>$this->app_id,'area_id'=>$area['id'],'hospital_id'=>$hid])
            ->select();
        //分店

        $fendian = $this->HospitalChildModel->where(['hospital_id'=>$hid])
            ->select();
        //获取科室
        $keshiHospital = $this->KeshiHospitalModel->with(['yyks'])->where(array(
            'hospitalid'=>$hid
        ))->select();
        $hospital['keshi'] = [];
        if(empty($keshiHospital) === FALSE) {
            foreach ($keshiHospital as $row) {
                $row->getRelation('yyks')->visible(['name']);
            }

            $hospital['keshi'] = $keshiHospital;
        }
		// 获取服务
		//$services = pdo_fetchall('SELECT id,code,name,logo,intro,price FROM ' .tablename('vp_pz_service') . ' where  uniacid = :uniacid AND status=1 AND is_del=0 order by sort desc ', array(':uniacid' => $_W['uniacid']));
		$services = $this->ServiceModel->field('id,code,stype,name,logo_image,icon_image,intro,price,priceo')
            ->where(['app_id'=>$this->app_id,'use_switch'=>1])->order('weigh', 'desc')->select();
        $resService = [];
		// 根据医院设置，覆盖服务
		for($i=0;$i<count($services);$i++){
			for($j=0;$j<count($hss);$j++){
				if($services[$i]['id']==$hss[$j]['service_id']){
					//$_service=$hospital['service'][$services[$i]['id']];
					$services[$i]['use_switch']=$hss[$j]['use_switch'];
					$services[$i]['price']=$hss[$j]['price'];
					//$services[$i]['logo']=VP_IMAGE_URL($services[$i]['logo']);
                    array_push($resService,$services[$i]);
				}
			}
		}
        $this->success('',array(
			'now'=>time(),
			'hospital'=>$hospital,
			'services'=>$resService,
            'fendian'=>$fendian
		));
    }
    //本地医院管理员登录手机端
    public function login(){
        $username = input("username",'');
        $password = input("password",'');
        if(empty($username) || empty($password)){
            $this->error('参数有误');
        }
        $user = $this->AdminModel::get(['username' => $username]);
        if (!$user) {
            $this->error('账号不存在');
            return false;
        }
        $checkpass = \app\common\library\Auth::instance()->getEncryptPassword($password, $user['salt']);
        if ($user->password != $checkpass) {
            $this->error('密码有误');
            return false;
        }
        //检测是否是本地医院的管理员
        $checkisLocal = $this->HospitalModel->where(array(
            'is_local'=>1,
            'admin_id'=>$user->id
        ))->find();
        if(empty($checkisLocal)){
            $this->error('该账号非本地医院管理员，请确认后登录');
            return false;
        }
        //获取医生已约时间
        $this->success('',$checkisLocal->toArray());
    }
}
